package kr.icg.mshtest2;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;


public class Icg_Msh_Test2Servlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(Icg_Msh_Test2Servlet.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
{
System.out.println("why this???");

}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		System.out.println("why this???");
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		if (user != null) {
			String authorEmail = user.getEmail();
			String authorNickName = user.getNickname();
			String content = URLDecoder.decode(req.getParameter("content"), "UTF-8");
			float latitude = Float.parseFloat(req.getParameter("latitude"));
			float longitude = Float.parseFloat(req.getParameter("longitude"));
			Date date = new Date();
			
			Mention mention = new Mention(authorEmail, authorNickName, content, latitude, longitude, date);
			
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				
				Query Query = pm.newQuery(Mention.class);
				Query.setFilter("authorEmail == '"+authorEmail+"'");
				Query.deletePersistentAll();
				
				pm.makePersistent(mention);
				resp.setContentType("text/html; charset=utf-8");
				resp.getWriter().println(content);
			} catch (Exception e) {
				System.out.println(e.toString());
				// TODO: handle exception
			} finally {
				pm.close();
			}
		}
	}
}
